package com.opener.web.spring.exception;

import com.opener.common.utils.JsonUtils;
import com.opener.web.spring.i18n.MessageSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.List;


/**
 * @author baboy
 */
public class AppBaseException extends RuntimeException {
	private static final long serialVersionUID = -2008323894680198140L;
	private static final Logger log = LoggerFactory.getLogger(AppBaseException.class);
	private int status = 0;
	public AppBaseException(int status, String msgKey, Object... args) {
		super(MessageSource.getMessage(msgKey,args));
		String declaringClass = null;
		int lineNumber = 0;
		String methodName = null;

		StackTraceElement tes[] = this.getStackTrace();
		if (tes != null && tes.length>0){
			StackTraceElement te = tes[0];
			declaringClass = te.getClassName();
			lineNumber = te.getLineNumber();
			methodName = te.getMethodName();
		}
		String sArgs = "";
		if(args != null && args.length>0){
			List l = Arrays.asList(args);
			sArgs = JsonUtils.toString(l);
		}
		log.error("HandleException|class:" + declaringClass + "|method:"+methodName+"|line:"+lineNumber+"|msg:"+msgKey+"|status:"+status+"|args:"+sArgs);
		this.status = status;
	}
	public String getErrorMessage() {
		return super.getLocalizedMessage();
	}
	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	
}
